import Immutable from 'immutable';

const defaultState = new Immutable.List();

export default function sysRoleReducer(state = defaultState, action) {
  switch(action.type) {
    case 'ERROR_SYSROLE':
      return action.error.status;
    case 'CREATE_SYSROLE_DATA':
      var data = action.data;
      if (data.success) {
          return state.concat(data.data);
      }else {
        return [];
      }
    case 'EDIT_SYSROLE_DATA':
      var data = action.data
      var list = state;
      list.map(function(sysRole) {
        if(sysRole.id == data.id){
          sysRole.name = data.name;
          sysRole.permissions = data.permissions;
        }
      })
      return list;
    case 'DELETE_SYSROLE_DATA':
      console.log(action.id);
      _.remove(state, function(sysRole) {
        console.log(sysRole.id, action.id)
        return action.id !== sysRole.id
      })
      return state;
    case 'LOAD_SYSROLE_DATA':
      var data = action.data;
      if (data.success) {
        return data.data;
      }else {
        return [];
      }
    default:
      return state;
  }
}
